package com.app.zenmewan.controller;

import com.app.zenmewan.commons.Actions;
import com.app.zenmewan.commons.Message;
import com.app.zenmewan.entity.Comment;
import com.app.zenmewan.entity.User;
import com.app.zenmewan.service.CommentService;
import com.app.zenmewan.service.UserService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 评论(Comment)表控制层
 *
 * @author makejava
 * @since 2020-05-02 20:09:03
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    /**
     * 服务对象
     */
    private static final Logger log = LoggerFactory.getLogger(CommentController.class);
    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;
    @Autowired
    private Actions actions;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Comment selectOne(Integer id) {
        return this.commentService.queryById(id);
    }

    @Getter
    @Setter
    @AllArgsConstructor
    class Vo {
        Comment comment;
        String avaterUrl;
        String nickname;
        int level;
        Date to_createdate;
        String to_nickname;

    }
    @PostMapping("comment/{token}")
    public ModelAndView comment(@RequestBody Comment comment, @PathVariable("token") String token){
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int result = actions.checkUid(token);
        if(result>0){
            Comment c = new Comment();
            c.setUid(result);
            this.userService.Jiajifen(result,10);
            c.setArtId(comment.getArtId());
            c.setDateComment(new Date());
            c.setContent(comment.getContent());
            this.commentService.insert(c);
            modelAndView.addObject(Message.TYPE,"SUCCESS");
        }
        return modelAndView;
    }
    @GetMapping("delete")
    public void delete(int id){
        this.commentService.deleteById(id);
    }
    /**
     * 回复评论
     * @param comment
     * @param token
     * @return
     */
    @PostMapping("return/{token}/{cid}")
    public ModelAndView reComment(@RequestBody Comment comment, @PathVariable("token") String token,@PathVariable("cid") int cid){
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int result = actions.checkUid(token);
        Comment toc = this.commentService.queryById(cid);
        if(toc.getUid()==result){
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS,"不能回复自己的评论!");
            return modelAndView;
        }
        if(result>0){

            Comment c = new Comment();
            c.setUid(result);
            this.userService.Jiajifen(result,5);
            c.setArtId(comment.getArtId());
            c.setDateComment(new Date());
            c.setContent(comment.getContent());
            c.setToId(cid);
            c.setToUid(toc.getUid());
            c.setToContent(toc.getContent());
            this.commentService.insert(c);
            modelAndView.addObject(Message.TYPE,"SUCCESS");
        }
        return modelAndView;
    }

    /**
     * 根据文章id获取id
     *
     * @param art_id
     * @return
     */
    @GetMapping("list/{artId}")
    public ModelAndView list(@PathVariable("artId") int art_id) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        Comment c = new Comment();
        c.setArtId(art_id);
        List<Comment> list = this.commentService.queryAll(c);
        if (list.size() == 0) {
            modelAndView.addObject("list", null);
            return modelAndView;
        } else {
            List<Vo> l = new ArrayList<>();
            for (Comment comment : list) {
                int uid = comment.getUid();
                User u = userService.queryById(uid);
                User to_u = new User();
                Date to_createdate = null;
                if(comment.getToUid()!=null){
                    to_u =  userService.queryById(comment.getToUid());
                    for (Comment c1 : list) {
                        //查询目标评论内容
                        if (c1.getId() == comment.getToId()) {
                            to_createdate = c1.getDateComment();
                            break;
                        }
                    }
                }
                int level = (int) Math.floor(Math.log(u.getPoint()) * 1.02);
                Vo v = new Vo(comment, u.getAvataurl(), u.getNickname(), level, to_createdate, to_u.getNickname());
                l.add(v);
            }
            modelAndView.addObject("list", l);
            return modelAndView;
        }
    }
}